define(['jquery', 'bootstrap', 'backend', 'table', 'form'], function ($, undefined, Backend, Table, Form) {

    var Controller = {
        index: function () {
            // 初始化表格参数配置
            Table.api.init({
                extend: {
                    index_url: 'goods/goods_discount/index' + location.search,
                    add_url: 'goods/goods_discount/add',
                    edit_url: 'goods/goods_discount/edit',
                    del_url: 'goods/goods_discount/del',
                    multi_url: 'goods/goods_discount/multi',
                    import_url: 'goods/goods_discount/import',
                    table: 'goods_discount',
                }
            });

            var table = $("#table");

            // 初始化表格
            table.bootstrapTable({
                url: $.fn.bootstrapTable.defaults.extend.index_url,
                pk: 'id',
                sortName: 'id',
                fixedColumns: true,
                fixedRightNumber: 1,
                columns: [
                    [
                        {checkbox: true},
                        {field: 'id', title: __('Id')},
                        {field: 'discount_no', title: __('Discount_no'), operate: 'LIKE'},
                        {field: 'name', title: __('Name'), operate: 'LIKE', table: table, class: 'autocontent', formatter: Table.api.formatter.content},
                        {field: 'discount_type', title: __('Discount_type'), searchList: {"1":__('Discount_type 1'),"2":__('Discount_type 2'),"3":__('Discount_type 3'),"4":__('Discount_type 4')}, formatter: Table.api.formatter.normal},
                        {field: 'discount_value', title: __('Discount_value'), operate:'BETWEEN'},
                        {field: 'condition_type', title: __('Condition_type'), searchList: {"1":__('Condition_type 1'),"2":__('Condition_type 2'),"3":__('Condition_type 3')}, formatter: Table.api.formatter.normal},
                        {field: 'condition_value', title: __('Condition_value'), operate:'BETWEEN'},
                        {field: 'apply_type', title: __('Apply_type'), searchList: {"1":__('Apply_type 1'),"2":__('Apply_type 2'),"3":__('Apply_type 3'),"4":__('Apply_type 4'),"5":__('Apply_type 5')}, formatter: Table.api.formatter.normal},
                        {field: 'apply_ids', title: __('Apply_ids'), operate: 'LIKE', table: table, class: 'autocontent', formatter: Table.api.formatter.content},
                        {field: 'supplier_id', title: __('Supplier_id')},
                        {field: 'start_time', title: __('Start_time'), operate:'RANGE', addclass:'datetimerange', autocomplete:false, formatter: Table.api.formatter.datetime},
                        {field: 'end_time', title: __('End_time'), operate:'RANGE', addclass:'datetimerange', autocomplete:false, formatter: Table.api.formatter.datetime},
                        {field: 'status', title: __('Status')},
                        {field: 'priority', title: __('Priority')},
                        {field: 'limit_type', title: __('Limit_type'), searchList: {"1":__('Limit_type 1'),"2":__('Limit_type 2'),"3":__('Limit_type 3')}, formatter: Table.api.formatter.normal},
                        {field: 'limit_value', title: __('Limit_value')},
                        {field: 'used_count', title: __('Used_count')},
                        {field: 'max_discount_amount', title: __('Max_discount_amount'), operate:'BETWEEN'},
                        {field: 'is_show', title: __('Is_show')},
                        {field: 'create_time', title: __('Create_time'), operate:'RANGE', addclass:'datetimerange', autocomplete:false, formatter: Table.api.formatter.datetime},
                        {field: 'update_time', title: __('Update_time'), operate:'RANGE', addclass:'datetimerange', autocomplete:false, formatter: Table.api.formatter.datetime},
                        {field: 'operate', title: __('Operate'), table: table, events: Table.api.events.operate, formatter: Table.api.formatter.operate}
                    ]
                ]
            });

            // 为表格绑定事件
            Table.api.bindevent(table);
        },
        add: function () {
            $('#c-apply_type').on('change',function(e){ 
                var apply_type = $(this).val();
                if(apply_type == 2){
                    $.get("goods/goods_category/index",function(res){
                        console.log(res)
                    })
                }
                if(apply_type == 3){
                    $.get("goods/goods/index",function(res){
                        console.log(res)
                    })
                }
            })
           
            Controller.api.bindevent();
        },
        edit: function () {
            Controller.api.bindevent();
        },
        api: {
            bindevent: function () {
                Form.api.bindevent($("form[role=form]"));
            }
        }
    };
    return Controller;
});
